var $table;  
var selectionIds = [];  //保存选中ids  
var sysBaseDataTypeField = dataObject.sysBaseDataTypeField;
var singleStatus;
var toolbar = "toolbar_dialog";
var isSelected = true;
$(function () {
	  var columns =[
                       {field: 'state', checkbox: true},
                       {field: 'baseTypeName' , title: sysBaseDataTypeField.baseTypeName, sortName:'BASE_TYPE_NAME' , sortable: true,halign: 'center',align: 'center'},
                       {field: 'parentName' , title: sysBaseDataTypeField.parentId, sortName:'PARENT_ID' , sortable: true,halign: 'center',align: 'center'},
                       {field: 'baseTypeKey' , title: sysBaseDataTypeField.baseTypeKey, sortName:'BASE_TYPE_KEY' , sortable: true,halign: 'center',align: 'center'},
                       //{field: 'type' , title: sysBaseDataTypeField.type, sortName:'TYPE' , sortable: true,halign: 'center',align: 'center'},
                       {field: 'status' , title: sysBaseDataTypeField.status, formatter: 'statusFormatter', sortName:'STATUS' , sortable: true,halign: 'center',align: 'center'},
                       {field: 'action', title: pageData.action, formatter: '$table.actionFormatter', events: 'actionEvents', clickToSelect: false, width: 150, halign: 'center', align: 'center'}
                   ];
    $table = new BSTable("table", 'sys/sysbasedatatype/list', columns,"toolbar",560,'typeId','TYPE_ID','sysbasedatatype');
    var queryParams = function(params) {            
         return {
         	       page: params.pageNumber,
         	       limit: params.pageSize,
         	       sidx: params.sortName,
                   order: params.sortOrder,
                   baseTypeName:vm.search.baseTypeName,
              //查询字段
             };
     };
	$table.setQueryParams(queryParams);
    $table.init();
});


var vm = new Vue({
	el:'#app',
	data:{
		showList: true,
		title: null,
		sysBaseDataType: {	
			parentId : -1
		},
		search:{
            baseTypeName:"",
		}
	},
	methods: {
		query: function () {
			defaultActions.query();
		},
		add: function(){
		    vm.sysBaseDataType={
		    		parentId : -1
		    };
			defaultActions.add(vm);
		},
		update: function (event, id) {
		    defaultActions.update(id, $("#table"),$path+"/sys/sysbasedatatype/info/", vm, function(r){
	        	vm["sysBaseDataType"] = r["sysBaseDataType"];
	        });
		},
		saveOrUpdate: function (event) {
		    $('#defaultForm').bootstrapValidator('validate');
			if($('#defaultForm').data('bootstrapValidator').isValid()){
				 var url = vm.sysBaseDataType.typeId == null || vm.sysBaseDataType.typeId == ''? $path+"/sys/sysbasedatatype/save" : $path+"/sys/sysbasedatatype/update";
				 defaultActions.saveOrUpdate(url ,vm, $("#defaultForm"),"sysBaseDataType",event);
			}
		},
		del: function (event, id) {
		    defaultActions.del(id, $path+"/sys/sysbasedatatype/delete", $("#table") ) ;
		},
		audit:function(status){
			defaultActions.audit(status , $path+"/sys/sysbasedatatype/audit?status="+status, vm);
		},
		reload: function (event) {
		    defaultActions.reload($("#table"),$("#defaultForm"),vm,"sysBaseDataType"); 
		},
		detail: function (event, id) {
			defaultActions.detail(vm, id, event);
		},
		sysBaseDataTypeAssiot: function(){
			selectionIds=[];
			singleStatus = true;
			selectionIds.push(vm.sysBaseDataType.parentId);
			var fun = function(dialogRef) {
                    var row=$.map($("#table_dialog").bootstrapTable('getSelections'),function(row){
                        return row ;
		    	    });
					var parentId,parentName='';
					if(row.length > 0){
							for(var i=0;i<row.length;i++){
								parentId=row[i].typeId;
								parentName=row[i].baseTypeName;
			    		}
					}else{
						vm.sysBaseDataType.parentId = -1;
					}
					vm.sysBaseDataType.parentId=parentId;
					vm.sysBaseDataType.parentName=parentName;
					$("input[name='parentId']").val(parentName).change();
                    dialogRef.close();
		      };
  	        defaultActions.assiot(sysBaseDataTypeField.parentId, '../dialog/sys/sysbasedatatype_dialog.html', fun);
        }
	}
});

/*表单校验 start*/
$('#defaultForm').bootstrapValidator({
    feedbackIcons: vail.feedbackIcons(),
    fields: {
        baseTypeName: {
            message: 'The  is not valid',
            validators: {
                notEmpty: {
                    message: '不能为空！'
                },
                stringLength: {
                    min: 1,
                    max: 30,
                    message: '长度必须在1和30之间！'
                },
                regexp:{
                    regexp : /^[^ ]+$/,
                    message:'输入不能有空格'
                },
                remote: {
                    url: $path + '/sys/sysbasedatatype/checkExist',//验证地址
                    message: sysBaseDataTypeField.baseTypeName + pageData.hasExists,//提示消息
                    delay :  500,//每输入一个字符，就发ajax请求，服务器压力还是太大，设置2秒发送一次ajax（默认输入一个字符，提交一次，服务器压力太大）
                    type: 'GET',//请求方式
                    dataType: "json",
                    data:{
                        value:function(){
                            if(vm.sysBaseDataType.baseTypeName == undefined){
                                vm.sysBaseDataType.baseTypeName = "";
                            }
                            return vm.sysBaseDataType.baseTypeName;
                        },
                        id:function(){
                            if(vm.sysBaseDataType.typeId == undefined){
                                vm.sysBaseDataType.typeId = "";
                            }
                            return vm.sysBaseDataType.typeId;
                        },
                        columnName: function () {
							return 'BASE_TYPE_NAME';
                        }
                    }
                }
            }
        },
        baseTypeKey: {
            message: 'The  is not valid',
            validators: {
                notEmpty: {
                    message: '不能为空！'
                },
                stringLength: {
                    min: 1,
                    max: 30,
                    message: '长度必须在1和30之间！'
                },
                regexp:{
                    regexp : /^[^ ]+$/,
                    message:'输入不能有空格'
                }
            }
        },
        describe: {
            stringLength: {
                min: 0,
                max: 255,
                message: '长度不超过255！'
            }
		}
    }
});
/*表单校验 end*/
